package com.tech.koufu.ui.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.tech.koufu.R;
import com.tech.koufu.action.RequestQueueSingleton;
import com.tech.koufu.model.ChooseStock;
import com.tech.koufu.model.TrackerUser;
import com.tech.koufu.tools.CookieStringRequest;
import com.tech.koufu.tools.KouFuTools;
import com.tech.koufu.tools.Statics;
import com.tech.koufu.ui.activity.menu.MyChooseStockActivity;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 自选股列表
 * @name: ChooseStockAdapter 
 * @description: 
 * @date 2015年7月8日 下午3:23:58
 * @author jl_luo
 */
public class ChooseStockAdapter extends BaseAdapter{
	private Context context;  
	private LayoutInflater mInflater = null; 
	public ArrayList<ChooseStock> datas;
//	private CookieStringRequest stringRequest;
//	public  RequestQueue mQueue; 
	
	public String userId;
	public String sessionID;
	
	public ChooseStockAdapter(Context context) {
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.datas = new ArrayList<ChooseStock>();
	}

	@Override
	public int getCount() {
		if(datas!=null){
			return datas.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) { 
		return null;
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;  
		//如果缓存convertView为空，则需要创建View  
		if(convertView == null)  
		{  
			holder = new ViewHolder();  
			//根据自定义的Item布局加载布局  
			convertView = mInflater.inflate(R.layout.item_choosestock, parent,false);
			holder.tv_stockname = (TextView)convertView.findViewById(R.id.tv_stockname);
			holder.tv_stockcode = (TextView)convertView.findViewById(R.id.tv_stockcode);
			holder.tv_newprice = (TextView)convertView.findViewById(R.id.tv_newprice);
			holder.tv_upanddown = (TextView)convertView.findViewById(R.id.tv_upanddown);
			holder.img_rm_choose = (ImageView)convertView.findViewById(R.id.img_rm_choose);
			convertView.setTag(holder);  
		}else{  
			holder = (ViewHolder)convertView.getTag();  
		}  
		final ChooseStock cs = datas.get(position);
		holder.tv_stockname.setText(cs.stockname);
		holder.tv_stockcode.setText(""+cs.stockcode);
		holder.tv_newprice.setText(cs.zx);
		holder.tv_upanddown.setText(cs.ztl);
		holder.tv_upanddown.setTextColor(context.getResources().getColor(KouFuTools.getStockValueColor(cs.ztl)));
		final int pos = position;
		holder.img_rm_choose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				datas.remove(position);
				removeChoose(cs, pos);
			}
		});
		return convertView;
	}
	
	private void removeChoose(final ChooseStock cs,final int position){
		String url = Statics.URL_PHP+Statics.IF_removechoose; 
		CookieStringRequest stringRequest = new CookieStringRequest(Method.POST, url, new Listener<String>() {

			@Override
			public void onResponse(String response) {   
				if(!TextUtils.isEmpty(response)){
					try {  
						JSONObject obj = new JSONObject(response);
						String status = obj.optString("status");
						String data = obj.optString("data");
						Toast.makeText(context, data, Toast.LENGTH_SHORT).show();
						if(status.equals("0")){
							 datas.remove(position);
							 ChooseStockAdapter.this.notifyDataSetChanged();
						}
					} catch (JSONException e) { 
						e.printStackTrace();
					}
				} 
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) { 
				
			}
		}){
			@Override
			protected Map<String, String> getParams() throws AuthFailureError { 
				Map<String, String> params = new HashMap<String, String>(); 
				params.put("stock_code", ""+cs.stockcode);   
				params.put("userID", userId);
				return params;
			}
		}; 
		stringRequest.setHeader(sessionID);
		stringRequest.setTag(MyChooseStockActivity.TAG);
		RequestQueueSingleton.getInstance(context).addToRequestQueue(stringRequest); 
	}
	
	public class ViewHolder  
	{   
		public ImageView img_rm_choose;
		public TextView tv_stockname;  
		public TextView tv_stockcode;   
		public TextView tv_newprice;
		public TextView tv_upanddown;

	}  
}
